import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isFunction from "lodash/isFunction";
var _excluded = ["value", "options", "onChange"];

var _dec, _dec2, _class;

/**
 * ScriptEdit.js
 * @date 2018/11/7
 */
import React from 'react';
import { Controlled as CodeMirror } from 'react-codemirror2';
import 'codemirror/mode/clike/clike';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/dracula.css';
import { Bind } from 'lodash-decorators';
var ScriptEdit = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(ScriptEdit, _React$Component);

  var _super = _createSuper(ScriptEdit);

  function ScriptEdit() {
    var _this;

    _classCallCheck(this, ScriptEdit);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.editor = void 0;
    return _this;
  }

  _createClass(ScriptEdit, [{
    key: "handleCodeMirrorRef",
    value: function handleCodeMirrorRef(editor) {
      this.editor = editor;
      editor.setSize('540px', '340px');
    }
  }, {
    key: "handleChange",
    value: function handleChange(editor, data, value) {
      var onChange = this.props.onChange;

      if (_isFunction(onChange)) {
        onChange(value);
      }
    }
  }, {
    key: "render",
    value: function render() {
      // 只有 onBeforeChange 才能 更新
      var _this$props = this.props,
          value = _this$props.value,
          options = _this$props.options,
          onChange = _this$props.onChange,
          otherProps = _objectWithoutProperties(_this$props, _excluded);

      return /*#__PURE__*/React.createElement(CodeMirror, Object.assign({}, otherProps, {
        autoScroll: true,
        value: value,
        onBeforeChange: this.handleChange,
        options: _objectSpread({
          mode: 'javascript',
          lineNumbers: true,
          autoFocus: true,
          cursorHeight: 0.85,
          viewportMargin: Infinity
        }, options),
        editorDidMount: this.handleCodeMirrorRef
      }));
    }
  }]);

  return ScriptEdit;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "handleCodeMirrorRef", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "handleCodeMirrorRef"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "handleChange", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "handleChange"), _class.prototype)), _class));
export { ScriptEdit as default };